<?php

if (!defined('PUBLIC_HTML'))
{
	exit;
}

class Validator  {
	
	// Check if something is really empty, including white spaces
	public function is_empty($value)
	{
		if (strlen(trim($postcode)) == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	// Check if something is numeric
	
	
	// Validate zip/postal codes
	public function postcode ($postcode, $optional=false)
    {
        if (!$optional)
        {
        	if ($this->is_empty($postcode))
        	{
        		return false;
        	}
        }
        elseif ($optional)
        {
	    	if (strlen(trim($postcode)) > 0)
	    	{
	        	if (strlen($postcode) == 6)
		        {
		    	    if (preg_match ("([1-9]{1}[0-9]{3}\s?[A-Za-z]{2})", $postcode))
		            {
		        	    return true;
				    }
		            else
		            {
		           		return false;
				    }
		    	}
		        else
		        {
		        	return false;
				}
	    	}
        }
    }
	
    
    // Validate phone
	public function phonenr ($phonenr, $optional=false) 
	{
		if (!$optional)
        {
        	if ($this->is_empty($phonenr))
        	{
        		return false;
        	}
        }
		
		if (is_numeric($phonenr) && strlen($phonenr) == 10) 
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
	
	// Validate house number
	public function houseno ($streetno, $optional=false) 
	{
		if (!$optional)
        {
        	if ($this->is_empty($streetno))
        	{
        		return false;
        	}
        }
		
		if (is_numeric($streetno) && strlen($streetno) >= 1) 
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
	
	// Validate E-mail
	public function email ($email, $optional=false) 
	{
		if (!$optional)
        {
        	if ($this->is_empty($email))
        	{
        		return false;
        	}
        }
		
		# First, we check that there's one @ symbol, and that the lengths are right
		if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
			
			# Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}
		
		# Split it into sections to make life easier
		$email_array = explode ("@", $email);
		$local_array = explode (".", $email_array[0]);
		
		
		
		for ($i = 0; $i < sizeof ($local_array); $i++) {
			
			if (!preg_match("/^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
				return false;
			}
	
		} 
		
		# top level domain
		$tld = end (explode ('.', $email_array[1]));
		
		if (!preg_match ("/^[A-z]{2,}$/", $tld)) { 
		
			return false; # tld must have at least 2
		
		}
	 
		if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
			
			$domain_array = explode (".", $email_array[1]);
			
			if (sizeof ($domain_array) < 2) {
				
				return false; # Not enough parts to domain
			}
			
			for ($i = 0; $i < sizeof ($domain_array); $i++) {
				
				if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
					
					return false;
				}	
			}
		}
		
		return true;
	} 
	
	// Check required fields
	public function check_required($fields, $type='')
	{
		global $_POST;
		
		if (is_array($fields))
		{
			foreach ($fields as $key => $value)
			{
				if (isset($value))
				{
					if (preg_match('/|optional/', $value))
					{
						$optional = true;
						$value_check = explode('|', $value);
					}
					else
					{
						$optional		= false;
						$value_check	= array();
						$value_check[0] = $value;
					}
					
					switch ($value_check[0])
					{
						case 'phone':
							return ($this->phonenr($_POST[$key], $optional))	? true : false;
						break;
						
						case 'email':
							return ($this->email($_POST[$key], $optional)) 	? true : false;
						break;
						
						case 'houseno':
							return ($this->houseno($_POST[$key], $optional)) 	? true : false;	
						break;
						
						case 'postcode':
							return ($this->postcode($_POST[$key], $optional))	? true : false;
						break;
						
						default:
							return ($_POST[$key] != '') ? true : false;
						break;
					}
				}
				else
				{
					return ($_POST[$key] != '') ? true : false;
				}
			}
		}
		else
		{
			switch ($value)
			{
				case 'phone':
					return ($this->phonenr($_POST[$key]))	? true : false;
				break;
				
				case 'email':
					return ($this->email($_POST[$key]))		? true : false;
				break;
				
				case 'houseno':
					return ($this->houseno($_POST[$key]))	? true : false;	
				break;
				
				case 'postcode':
					return ($this->postcode($_POST[$key]))	? true : false;
				break;
				
				default:
					return ($_POST[$key] != '') ? true : false;
				break;
			}
		}
	}
}


?>